<?php

/**
 * 
 * @author littlexiang
 * @version $Id: ShopController.php 62 2009-07-22 04:45:58Z littlexiang521 $
 */
class ShopController extends Controller {
    /**
     * Enter description here...
     *
     */
    public function indexAction () {
        $model_shop = new ShopModel();
        $model_station = new StationModel();
        $model_tag = new TagModel();
        
        $order = Context::getParam('order');
        $tag_name = Context::getParam('tag');
        
        if ($line = Context::getParam('line')) {
            
            $this->stations = $model_station->getStationsByLine($line);
            
        } elseif ($station = Context::getParam('station')) {
            
            $this->stations = $model_station->getStationsOfSameLine($station);
        
        }
        
        $this->list = $model_shop->getShops($line, $station, $tag_name, $order);
        
        $this->tags = $model_tag->getTags();
        $this->lines = $model_station->getLines();
        
        return 'shop/index';
    }
    
    /**
     * Enter description here...
     *
     */
    public function viewAction () {
        $id = intval(Context::getParam('id'));
        
        $shop = new ShopModel();
        
        $this->shop = $shop->getShopById($id);
        
        $this->goods = $shop->getShopGoods($id);
        
        $this->tags = $shop->getShopTags($id);
        
        $page = new Page();
        
        $page->total = $shop->countShopComment($id);
        $this->comments = $shop->getShopComments($id, $page->getLimit());
        $this->page = $page;
        
        $this->form = $this->_buildCommentForm($id);
        
        $shop->viewPlus($id);
        return 'shop/view';
    
    }
    
    /**
     * Enter description here...
     *
     */
    public function commentAction () {
        $shop_id = Context::getParam('id');
        
        $form = $this->_buildCommentForm($shop_id);
        $form->setValue($_POST);
        if ($form->isValid()) {
            $value = $form->getValue();
            $comment = new CommentModel();
            $insert['title'] = $value['title'] . '[评分(' . $value['score'] . ')]';
            $insert['user_id'] = $_SESSION['user_id'] ? $_SESSION['user_id'] : 0;
            $insert['shop_id'] = $shop_id;
            $insert['content'] = $value['content'];
            $insert['create_time'] = time();
            
            $comment->score($shop_id, $value['score']);
            if ($comment->addComment($insert)) {
                $this->redirect('shop/view', array(
                    'id' => $shop_id , 
                    'msg' => '评论成功'));
            } else {
                $this->redirect('shop/view', array(
                    'id' => $shop_id , 
                    'msg' => '评论失败'));
            }
        }
    
    }
    /**
     * @return Form
     */
    private function _buildCommentForm ($shop_id) {
        $form = new Form('comment', array(
            'method' => 'post' , 
            'action' => $this->makeUri('shop/comment', array(
                'id' => $shop_id))));
        
        $filter = new Filter();
        $filter->addFilter(new Filter_Html());
        
        $title = new Form_Input('title', array(
            'type' => 'text' , 
            'length' => 50 , 
            'label' => '标题'));
        $title->addFilter($filter);
        
        $score = new Form_Input('score', array(
            'type' => 'text' , 
            'length' => 50 , 
            'label' => '评分'));
        $title->addFilter($filter);
        
        $content = new Form_Textarea('content', array(
            'type' => 'text' , 
            'length' => 50 , 
            'label' => '评论'));
        $content->addFilter($filter);
        
        $form->addElement($title);
        $form->addElement($content);
        $form->addElement($score);
        
        $form->addElement(new Form_Input('submit', array(
            'type' => 'submit'), '提交'), true);
        $form->addElement(new Form_Input('reset', array(
            'type' => 'reset'), '重置'), true);
        
        return $form;
    }
}
?>